#include "StdAfx.h"
#include "UIUtility.h"

CEGUI::String ScriptBackend::UIUtility::toCeguiString( const std::string& src )
{
	const char* c = src.c_str();
	static CEGUI::String str;
	WCHAR e;
	str.clear();
	int i = 0;
	while(c[i])
	{
		if(unsigned char(c[i])>=161)
		{ 
			MultiByteToWideChar(0,0,c+i,2,&e,1);
			str.append(1,e);
			i+=2;
		} 
		else
		{
			str.append(1,c[i]);
			i++;
		}
	}
	return str;
}

std::string ScriptBackend::UIUtility::toStdString( const CEGUI::String& str )
{
	const char* chTemp = str.c_str();
	WCHAR*     strA;      

	int i= MultiByteToWideChar(CP_UTF8,0 ,(char*)chTemp,-1,NULL,0); 
	strA = new WCHAR[i];       
	MultiByteToWideChar(CP_UTF8,0,(char*)chTemp,-1,strA,i);   

	i= WideCharToMultiByte(CP_ACP,0,strA,-1,NULL,0,NULL,NULL);
	char *strB = new char[i];
	WideCharToMultiByte (CP_ACP,0,strA,-1,strB,i,NULL,NULL);

	std::string strReturn = std::string(strB);
	delete strA;
	delete strB;

	return strReturn;
}